﻿using Knight.Models.Entity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Knight.Models.Base
{
    public class DataContext : DbContext
    {
        public readonly static string connectionStr = "Data Source=.;Initial Catalog=HS_DB;User ID=sa;Password=123456";

        public DataContext() : base("default")
        {
            //配置数据库链接
            //Database.Connection.ConnectionString = connectionStr;
            Database.SetInitializer<DataContext>(null);

        }
        public DataContext(string connection) :base("default")
        {
            Database.Connection.ConnectionString = connection;
            Database.SetInitializer<DataContext>(null);
        }


        /// <summary>
        /// 添加数据库和实体的映射
        /// </summary>
        /// <param name="modelBuilder">将CLR类型映射到数据库</param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //表         
            modelBuilder.Configurations.Add(new AdministratorsMapping());
            modelBuilder.Configurations.Add(new LinkInfoMapping());
            modelBuilder.Configurations.Add(new LogInfoMapping());
            modelBuilder.Configurations.Add(new NewsInfoMapping());
            modelBuilder.Configurations.Add(new NewsTypeMapping());
            modelBuilder.Configurations.Add(new RoleInfoMapping());
            modelBuilder.Configurations.Add(new SettingMapping());
            modelBuilder.Configurations.Add(new SinglePageInfoMapping());
            modelBuilder.Configurations.Add(new CouponInfoMapping());
            modelBuilder.Configurations.Add(new ClassACommodityMapping());

        }


        public void FixEfProviderServicesProblem()
        {
            //The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'  
            //for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.   
            //Make sure the provider assembly is available to the running application.   
            //See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.  

            var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
        }

    }
}

